package com.wlyuan.open.remote.consumer;

import com.wlyuan.core.domain.event.DomainEventPublisher;
import com.wlyuan.open.infrastructure.event.OrderChanged;
import com.wlyuan.open.remote.converter.OrderMessageConverter;
import com.wlyuan.open.remote.message.OrderMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class OrderDeviceUnboundConsumer {

    private final OrderMessageConverter messageConverter = new OrderMessageConverter();

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "queue.open.pusher.order.unbound"),
                    exchange = @Exchange(value = "exchange.topic.order"), key = "OrderDeviceUnbound")
    })
    public void receiveOrderCreated(String message) {
        logger.info("接到订单解绑设备消息：{}",message);
        OrderMessage orderSignedMessage = messageConverter.convert(message);
        //转发
        DomainEventPublisher.publish(new OrderChanged(orderSignedMessage.getOrder(),"OrderDeviceUnbound"));
    }
}
